(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev.device')
        .controller('DeviceListCtrl', DeviceListCtrl);

    /** @ngInject */
    function DeviceListCtrl($scope, $filter, toastr, DeviceService, DictService) {
        var kt = this;
        kt.devicelist = [];
        kt.addRow = function () {
            kt.inserted = {
                id: null,
                code: null,
                remark: null,
            };
            kt.dictlist.push(kt.inserted);
        };

        kt.getDictLabel = DictService.getDictLabel;

        kt.save = function (dict) {
            if (dict.code == null || dict.code == '') {
                toastr.warning('编号不能为空', "提示:", {"progressBar": true,});
                return;
            }
            DeviceService.save(dict,function (data) {
                kt.LoadPage();
            });
        };

        kt.LoadPage = function (tableState) {
            tableState = tableState || kt.tableState;
            tableState.pagination.number = tableState.pagination.number || 5;
            DeviceService.getSmartData(tableState,
                function (data) {
                    tableState.pagination.numberOfPages = data.result.pages;
                    tableState.pagination.totalItemCount = data.result.total;
                    kt.tableState = tableState;
                    kt.devicelist = data.result.records;

                    // 显示设备点
                    if (kt.map) {
                        // 删除覆盖点
                        var allOverlays = kt.map.getOverlays();
                        for (var i = 0; i < allOverlays.length; i++) {
                            kt.map.removeOverlay(allOverlays[i]);
                        }
                
                        angular.forEach(kt.devicelist, function (dev) {
                            if (dev.lng && dev.lat) {
                                // 添加新的覆盖点
                                var marker = new BMap.Marker(new BMap.Point(dev.lng, dev.lat));
                                kt.map.addOverlay(marker);
                            }
                        });
                    }
                });
        };

        //删除
        kt.del = function (id) {
            if(id==null){
                kt.LoadPage();
                return;
            }
            DeviceService.del({id: id},
                function (data) {
                    kt.LoadPage();
                })
        };
        kt.checkboxes = {
            checked: false,
            items: {}
        };
        $scope.$watch('kt.checkboxes.checked', function (value) {
            angular.forEach(kt.dictlist, function (item) {
                kt.checkboxes.items[item.id] = value;
            });
        });


        kt.initBap = function () {
            console.log('初始化地图');
            kt.map = new BMap.Map("bmap-con");
            kt.map.centerAndZoom(new BMap.Point(121.48, 31.22), 6);  // 初始化地图，设置中心点坐标和地图级别
            kt.map.addControl(new BMap.NavigationControl());
            kt.map.addControl(new BMap.ScaleControl());
            kt.map.addControl(new BMap.OverviewMapControl());
            kt.map.enableScrollWheelZoom(true);

            // 去除路网
            //去除路网
            kt.map.setMapStyle({
                styleJson:[
                {
                    "featureType": "road",
                    "elementType": "all",
                    "stylers": {
                        "color": "#ffffff",
                        "visibility": "off"
                        }
                  }
                ]
            });
        }

        // 页面完成后加载 地图
        $scope.$watch('$viewContentLoaded', function() {  
            kt.initBap();
        }); 
    }

})();
